package com.wisdytech.common.thrift.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisdytech.common.enums.ActionType;
import com.wisdytech.common.thrift.api.DCSService;
import com.wisdytech.common.thrift.dto.ServerDto;
import com.wisdytech.common.thrift.service.IEquipmentMqDcsService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipment.dao.IEqClientDao;
import com.wisdytech.linkdcs.equipment.dao.IEqToClientDao;
import com.wisdytech.linkdcs.equipment.model.EquipmentMQ;
import com.wisdytech.linkdcs.equipment.model.EquipmentToClient;
import com.wisdytech.linkdcs.equipment.service.IEquipmentMqService;


@Service
@Transactional
public class EquipmentMqServiceImpl implements IEquipmentMqDcsService{

	private static Logger logger =LoggerFactory.getLogger(EquipmentMqServiceImpl.class);
    
    @Resource
    private IEqClientDao clientDao;
    
    @Resource
    private IEqToClientDao eqToClientDao;
    
    @Autowired
    IEquipmentMqService IEquipmentMqService;
	
	@Override
	public void add(String eqCollectionId) {
		//查找相关对象
		EquipmentToClient etc=
				eqToClientDao.selectCollectionDetailById(eqCollectionId);
		
		ServerDto server = BaseUtil.getServerAddress(etc.getClientRoute());
        if(null == server){
            return ;
        }
		
		//查询队列参数
		List<EquipmentMQ> mqLists=
				IEquipmentMqService.getModelMqList(etc.getModelId());
		
		Map<String,List<String>> paramsMaps=new HashMap<>();
		for(EquipmentMQ mq : mqLists) {
			String mqName =mq.getMqName();
			if(paramsMaps.containsKey(mqName)) {
				List<String> params =paramsMaps.get(mqName);
				params.add(mq.getParamId());
			}else {
				List<String> params =new ArrayList<>();
				params.add(mq.getParamId());
				paramsMaps.put(mqName, params);
			}
		}
		
		for(String key :paramsMaps.keySet()) {
			sentToDCS(etc.getModelCode(), key, paramsMaps.get(key), server, ActionType.ADD);
		}
		
		
	}

	@Override
	public void update(String eqCollectionId) {
		//查找相关对象
		EquipmentToClient etc=
				eqToClientDao.selectCollectionDetailById(eqCollectionId);
		
		ServerDto server = BaseUtil.getServerAddress(etc.getClientRoute());
        if(null == server){
            return ;
        }
		
		//查询队列参数
		List<EquipmentMQ> mqLists=
				IEquipmentMqService.getModelMqList(etc.getModelId());
		
		Map<String,List<String>> paramsMaps=new HashMap<>();
		for(EquipmentMQ mq : mqLists) {
			String mqName =mq.getMqName();
			if(paramsMaps.containsKey(mqName)) {
				List<String> params =paramsMaps.get(mqName);
				params.add(mq.getParamId());
			}else {
				List<String> params =new ArrayList<>();
				params.add(mq.getParamId());
				paramsMaps.put(mqName, params);
			}
		}
		
		for(String key :paramsMaps.keySet()) {
			sentToDCS(etc.getModelCode(), key, paramsMaps.get(key), server, ActionType.ADD);
		}
		
	}

	@Override
	public void del(String eqCollectionId) {
		//查找相关对象
		EquipmentToClient etc=
				eqToClientDao.selectCollectionDetailById(eqCollectionId);
		
		ServerDto server = BaseUtil.getServerAddress(etc.getClientRoute());
        if(null == server){
            return ;
        }
		
		//查询队列参数
		List<EquipmentMQ> mqLists=
				IEquipmentMqService.getModelMqList(etc.getModelId());
		
		Map<String,List<String>> paramsMaps=new HashMap<>();
		for(EquipmentMQ mq : mqLists) {
			String mqName =mq.getMqName();
			if(paramsMaps.containsKey(mqName)) {
				List<String> params =paramsMaps.get(mqName);
				params.add(mq.getParamId());
			}else {
				List<String> params =new ArrayList<>();
				params.add(mq.getParamId());
				paramsMaps.put(mqName, params);
			}
		}
		
		for(String key :paramsMaps.keySet()) {
			sentToDCS(etc.getModelCode(), key, paramsMaps.get(key), server, ActionType.ADD);
		}
		
	}
	
	/**
     * <p>Description: 调用dcs</p>
     * @param modelCode 设备型号编码
     * @param equipmentCode 设备编码
     * @param clientId 终端id
     * @param server 终端连接信息
     * @param type 推送状态
     * @return
     */
    private boolean sentToDCS(String modelCode,String mqName,List<String> params,
            ServerDto server,ActionType type){
        
        TTransport tTransport = null;
        try {
            tTransport = new TSocket(server.getIp(),server.getPort());
            tTransport.open();
            TProtocol protocol=new TBinaryProtocol(tTransport);
            DCSService.Client client =new DCSService.Client(protocol);

            client.putMqInfo(mqName, modelCode, params, type.getCode(), System.currentTimeMillis());

            return true;
            
        } catch (TTransportException e) {
        	logger.error("推送设备信息失败 ",e);
            e.printStackTrace();
        } catch (TException e) {
        	logger.error("推送设备信息失败 ",e);
            e.printStackTrace();
        }finally{
            if(null != tTransport){
                tTransport.close();
            }
        }
        return false;
        
    }
	
	

}
